<?php
class plugin extends Admin {

    private $result = null; // 插件列表
    private $status = array( '激活', '禁用' );

    public function  __construct() {
        parent::__construct();
        $this->result = $this->model->Cache()->get( 'plugin' );
    }

    public function index( $c, $a ) {
        $package = list_file( APP_PLUGIN, "zip" );
        include $this->display( $c );
    }

    // 插件配置页面
    public function config( $c ) {

        if ( !$id = intval( R( 'id' ) ) ) {
            $this->error();
        }
        if ( !$plugin = $this->result[ $id ] ) {
            $this->error();
        }

        $config = APP_PLUGIN . "{$plugin['pid']}/{$plugin['pid']}";
        if ( is_file( $config . "_config.php" ) ) {
            $content = file_get_contents( $config . "_config.php" );
            $content = unserialize( substr( $content, 14 ) );
        }
        if ( is_file( $config . "_var.php" ) ) require_once $config . "_var.php";
        include $this->display( "header" );
        include $this->display( $config . "_config.html" );
        include $this->display( "footer" );
    }

    // 运行插件
    public function run( $c ) {
        if ( !$id = intval( R( 'id' ) ) ) {
            $this->error();
        }
        if ( !$plugin = $this->result[ $id ] ) {
            $this->error();
        }

        $file = APP_PLUGIN . "{$plugin['pid']}/{$plugin['pid']}.php";
        if ( !$plugin[ 'status' ] || !is_readable( $file ) ) {
            $this->error( "插件已被禁用或插件文件不存在!" );
        }

        require_once $file;
        $obj = C( $plugin[ 'pid' ] );
        $obj->run();
    }

    // 保存插件配置
    public function save() {
        if ( !$id = intval( R( 'id' ) ) ) {
            $this->error();
        }
        if ( !$plugin = $this->result[ $id ] ) {
            $this->error();
        }
        $data = "<?php die();?>" . serialize( $_POST );
        $file = APP_PLUGIN . "{$plugin['pid']}/{$plugin['pid']}_config.php";

        if ( !is_writable_dir( APP_PLUGIN . "{$plugin['pid']}/" ) ) {
            $this->error( '插件目录' . APP_PLUGIN . "{$plugin['pid']}/" . '不可写!' );
        }

        if ( file_put_contents( $file, $data ) ) {
            chmod( $file, 0666 );
            $this->success( '插件配置保存成功!' );
        }
    }

    # 激活/禁用 插件
    public function active( $c, $a ) {
        if ( !$id = intval( R( 'id' ) ) ) {
            $this->error();
        }

        $this->model->table = $c;
        $conditions = array( 'id' => $id );
        $this->model->update( $conditions, array( 'status' => R( 'v' ) == 1 ? 0 : 1 ) );

        # 更新插件缓存
        $this->model->Cache()->update( $this->table );

        $this->success( "插件{$this->status[R( 'v' )]}成功!" );
    }

    // 安装插件
    public function install( $c, $a ) {
        $id = R( 'package' );
        $file = APP_PLUGIN . "{$id}.zip";
        $info = $this->Zip()->decompress( $file, APP_PLUGIN );
        if ( !$info ) {
            $this->error( '安装包解压失败!' . $file );
        }

        $plugin_file = APP_PLUGIN . "{$id}/{$id}.php";

        if ( !file_exists( $plugin_file ) ) {
            $this->error( '非法插件包!' );
        }

        require_once $plugin_file;

        $obj = C( $id );
        if ( !method_exists( $obj, 'version' ) ) {
            $this->error( '插件信息丢失!' );
        }

        $version = $obj->version();

        # 写入插件到数据库
        $this->model->table = $c;
        if ( $this->model->find( array( 'pid' => $version[ 'pid' ] ) ) ) {
            $this->error( "插件已经安装过了。" );
        }
        $this->model->create( $version );

        // 删除安装包
        @unlink( $file );

        // 更新插件缓存
        $this->model->Cache()->update( $this->table );

        $this->success( '插件安装成功!' );

    }
}